package org.uned.agonzalo16.bitacora.dao;

import static org.uned.agonzalo16.bitacora.dao.OfyFactory.ofy;

import java.util.List;

import org.springframework.stereotype.Repository;
import org.uned.agonzalo16.bitacora.domain.Message;
import org.uned.agonzalo16.bitacora.domain.User;

@Repository
public class MessageDao {

	public List<Message> findAll() {
		return ofy().load().type(Message.class).list();
	}

	public List<Message> findByOrigin(User user) {
		return ofy().load().type(Message.class).filter("origin", user).order("-creationDate").list();
	}

	public List<Message> findByDestination(User user) {
		return ofy().load().type(Message.class).filter("destination", user).order("-creationDate").list();
	}

	public List<Message> findNotReadByDestination(User user) {
		return ofy().load().type(Message.class).filter("destination", user).filter("read", Boolean.FALSE).order("-creationDate").list();
	}

	public Message get(Long id) {
		return ofy().load().type(Message.class).id(id).now();
	}

	public Message merge(Message msg) {
		ofy().save().entity(msg).now();
		return msg;
	}

	public void delete(Long id) {
		ofy().delete().type(Message.class).id(id).now();
	}

}
